#include "Util.h"
#include "hgeVector.h"
#include "hgeRect.h"
#include "BoundingCircle.h"

namespace Util
{
	float GetRandomFloat(float lowBound, float highBound)
	{
		if(lowBound >= highBound)
			return lowBound;
		float f = (rand() % 10000) * 0.0001f;
		return (f * (highBound - lowBound) + lowBound);
	}
	bool MouseCollision(hgeVector a_objectDimensions, hgeVector a_objectPos, hgeVector a_mousePos)
	{
		//Translate by the position of the rect so that its not in local space anymore
		hgeRect rect;
		rect.x1 = a_objectPos.x - (a_objectDimensions.x/2);
		rect.x2 = a_objectPos.x + (a_objectDimensions.x/2);
		rect.y1 = a_objectPos.y - ((a_objectDimensions.y/2)/2);
		rect.y2 = a_objectPos.y + ((a_objectDimensions.y/2)/2);

		//Check to see if the mouse pos is outside the box
		if(rect.x1 > a_mousePos.x)
			return false;
		if(rect.x2 < a_mousePos.x)
			return false;
		if(rect.y1 > a_mousePos.y)
			return false;
		if(rect.y2 < a_mousePos.y)
			return false;

		//If it made it through all those checks, it is inside the box, return true, collision found;
		return true;
	}
	bool MouseCollision(hgeRect a_objectRect, hgeVector a_objectPos, hgeVector a_mousePos)
	{
		//Translate by the position of the rect so that its not in local space anymore
		TranslateRect(a_objectRect, a_objectPos);

		//Check to see if the mouse pos is outside the box
		if(a_objectRect.x1 > a_mousePos.x)
			return false;
		if(a_objectRect.x2 < a_mousePos.x)
			return false;
		if(a_objectRect.y1 > a_mousePos.y)
			return false;
		if(a_objectRect.y2 < a_mousePos.y)
			return false;

		//If it made it through all those checks, it is inside the box, return true, collision found;
		return true;
	}
	bool BoxBoxCollision(hgeRect a_lhs, hgeRect a_rhs)
	{
		//Check to see if the box is outside the other box
		if(a_lhs.x1 > a_rhs.x2)
			return false;
		if(a_lhs.x2 < a_rhs.x1)
			return false;
		if(a_lhs.y1 > a_rhs.y2)
			return false;
		if(a_lhs.y2 < a_rhs.y1)
			return false;

		//If it made it through all those checks, it is inside the box, return true, collision found;
		return true;
	}
	bool CircleCircleCollision(BoundingCircle a_lhs, hgeVector a_lhsPos, BoundingCircle a_rhs, hgeVector a_rhsPos)
	{
		hgeVector dist = a_lhsPos - a_rhsPos;
		//If the distance between them is less than or equal to the sum of the radii
		if(dist.Length() <= (a_lhs.radius + a_rhs.radius))
		{
			return true;
		}
		return false;
	}
	bool CircleBoxCollision(BoundingCircle a_lhs, hgeVector a_lhsPos, hgeRect a_rhs, hgeVector a_rhsPos)
	{
		a_rhs = TranslateRect(a_rhs, a_rhsPos);
		//If the circle is outside the top of the screen
		if(a_lhs.radius + a_lhsPos.y < a_rhs.y1)
			return false;
		if(a_lhs.radius - a_lhsPos.y > a_rhs.y2)
			return false;
		if(a_lhs.radius + a_lhsPos.x < a_rhs.x1)
			return false;
		if(a_lhs.radius - a_lhsPos.x > a_rhs.x2)
			return false;

		//Made it through the above checks, collision found
		return true;
	}
	hgeRect TranslateRect(hgeRect& a_rect, hgeVector a_pos)
	{
		a_rect.x1 += a_pos.x;
		a_rect.x2 += a_pos.x;
		a_rect.y1 += a_pos.y;
		a_rect.y2 += a_pos.y;

		return a_rect;
	}
	float AngleBetweenHgeVects(hgeVector a_object1, hgeVector a_object2)
	{
		hgeVector diff = a_object2 - a_object1;
		//calc. hypotenuse using pythagoras' theorem
		float diffLength = sqrt(diff.x * diff.x + diff.y * diff.y);
		//sin(theta) = opp/hypot.
		float angle = asin(diff.y / diffLength);
		return angle;
	}
	void FloatToTime(float a_time, int &a_hour, int &a_minute, int &a_second, int &a_millisecond)
	{
		a_hour = (int)a_time/3600;//convert from seconds to hours
		a_minute = (int)(a_time-(3600*a_hour))/60;//take away 3600 for every hour there is
		a_second = (int)(a_time-((3600*a_hour)+(60*a_minute))); //take away 60 seconds for every minute there is
		a_millisecond = (int)((a_time-((3600*a_hour)+(60*a_minute)+a_second))*100);
	}
}